// input：-123456.123
// output：-123,456.123

function thousandSeparator(n) {
    let nStr = n.toString();
    let [integerPart, decimalPart = ''] = nStr.split('.');
    if(integerPart[0] === '-'){
        integerPart = integerPart.slice(1);// 切割索引为1后面的字符串，并返回，slice是不会改动原来字符串的
    }
    // 定义变量，进行存储添加,后的结果
    let formattedIntegerPart = '';
    // 进行反转添加
    for (let i = integerPart.length - 1, count = 0; i >= 0; i--, count++) {
        if (count === 3) {
            formattedIntegerPart = ',' + formattedIntegerPart;
            count = 0;
        }
        formattedIntegerPart = integerPart[i] + formattedIntegerPart;
    }
    return (n < 0 ? '-' : '') + formattedIntegerPart + (decimalPart ? '.' + decimalPart : '');

}

console.log(thousandSeparator('123456.89'))
